package tenantchannellogic

import (
	"context"
	tenantChannel "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/tenant/channel"
	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type WavepayListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewWavepayListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WavepayListLogic {
	return &WavepayListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *WavepayListLogic) WavepayList(in *rpc.WavepayListRequest) (*rpc.WavepayListResponse, error) {
	var (
		CommonResp  rpc.CommonResp
		wavepayList []schema.TenantChannelWavepay
		err         error
	)

	wavepayModel := tenantChannel.NewTenantChannelWavepayModel(l.ctx, l.svcCtx.Db)
	if in.Code != "" {
		wavepayList, err = wavepayModel.GetWavepayByCode(in.Code)
	} else {
		wavepayList, err = wavepayModel.GetAllWavepay()
	}
	if err != nil {
		CommonResp.Code = xerrors.DBError
		CommonResp.Message = "Failed to fetch Wavepay list"
		return &rpc.WavepayListResponse{
			CommonResp: &CommonResp,
		}, err
	}

	var records []*rpc.Wavepay
	for _, wavepay := range wavepayList {
		records = append(records, &rpc.Wavepay{
			Id:           wavepay.ID,
			CreatedAt:    wavepay.CreatedAt,
			UpdatedAt:    wavepay.UpdatedAt,
			Code:         wavepay.Code,
			Phone:        wavepay.Phone,
			CardHolder:   wavepay.CardHolder,
			MinAmount:    wavepay.MinAmount,
			MaxAmount:    wavepay.MaxAmount,
			DailyTimes:   wavepay.DailyTimes,
			DailyAmount:  wavepay.DailyAmount,
			RemainTimes:  wavepay.RemainTimes,
			RemainAmount: wavepay.RemainAmount,
			VisionLevel:  wavepay.VisionLevel,
			IsDisplay:    wavepay.IsDisplay,
		})
	}

	CommonResp.Message = "Success"
	return &rpc.WavepayListResponse{
		Records:    records,
		CommonResp: &CommonResp,
	}, nil
}
